import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:ichinesereader/constants.dart';
import 'package:ichinesereader/controller/reports.dart';
import 'package:ichinesereader/pages/common/report/reportsStudentPersonalContent.dart';
import 'package:ichinesereader/themes/appTheme.dart';
import 'package:ichinesereader/utils/log.dart';
import 'package:ichinesereader/widgets/grids/bookSliderGridNoBar2.dart';
import 'package:ichinesereader/widgets/grids/currentHomeworkDataGrid.dart';
import 'package:ichinesereader/widgets/grids/studentStatisticsDataGrid.dart';
import 'package:percent_indicator/linear_percent_indicator.dart';

class ReportStudentPersonalOverview extends StatefulWidget {
  ReportStudentPersonalOverview({
    Key? key,
    required this.userName,
  }) : super(key: key);
  final String userName;

  @override
  State<ReportStudentPersonalOverview> createState() =>
      ReportStudentPersonalOverviewState();
}

class ReportStudentPersonalOverviewState
    extends State<ReportStudentPersonalOverview> {
  final ReportsController _reportsController = Get.find();

  @override
  void initState() {
    studentPersonalReportTab = Constants.studentPersonalReportTabs[0];

    fetchData(widget.userName);
    // Future.delayed(Duration(milliseconds: 600), (() {
    //   _reportsController.fetchStudentStatics(widget.userName);
    //   _reportsController.fetchStudentHistoryResults(widget.userName);
    // }));
    super.initState();
  }

  void fetchData(String userName) {
    Future.delayed(Duration(milliseconds: 200), (() {
      _reportsController.fetchStudentStatics(userName);
      _reportsController.fetchStudentHistoryResults(userName);
    }));
  }

  @override
  Widget build(BuildContext context) {
    Log.debug('build ReportStudentPersonalOverview');

    return SingleChildScrollView(
        scrollDirection: Axis.vertical,
        padding: const EdgeInsets.all(6.0),
        child: Container(
            margin: EdgeInsets.only(top: 12.0),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                //Level Bar here
                Container(
                  padding: EdgeInsets.all(9.0),
                  decoration: BoxDecoration(
                    color: Get.isDarkMode
                        ? AppTheme.colors.darkTheme2
                        : Colors.white.withOpacity(0.3),
                    borderRadius: BorderRadius.circular(3.0),
                  ),
                  child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Row(
                          children: [
                            Text(
                              "${_reportsController.classstudentreport.value.student != null ? _reportsController.classstudentreport.value.student!.level : 'level'}"
                                  .tr,
                              style: Theme.of(context)
                                  .textTheme
                                  .titleMedium
                                  ?.copyWith(
                                    fontSize: 15,
                                    color: Get.isDarkMode
                                        ? Get.theme.primaryColorDark
                                        : Colors.black,
                                  ),
                            ),
                            Expanded(child: _reportsController.obx((data) {
                              if (_reportsController
                                      .classstudentreport.value.student !=
                                  null) {
                                return Container(
                                  width: double.infinity,
                                  child: LinearPercentIndicator(
                                    animation: true,
                                    lineHeight: 18.0,
                                    animationDuration: 1000,
                                    //width: 300,
                                    percent: _reportsController
                                            .classstudentreport
                                            .value
                                            .student!
                                            .levelPercentage!
                                            .toDouble() /
                                        100,
                                    center: Text(
                                      _reportsController.classstudentreport
                                              .value.student!.levelPercentage
                                              .toString() +
                                          '%',
                                      style: TextStyle(
                                          color: Get.isDarkMode
                                              ? Get.theme.primaryColorDark
                                              : Colors.black),
                                    ),
                                    progressColor: Get.isDarkMode
                                        ? Get.theme.primaryColorDark
                                        : Get.theme.primaryColorLight
                                            .withOpacity(0.9),
                                    barRadius: Radius.circular(18),
                                    backgroundColor: Get.isDarkMode
                                        ? Colors.black
                                        : Colors.black.withOpacity(0.1),
                                  ),
                                );
                              } else {
                                return Text('');
                              }
                            }))
                          ],
                        ),
                      ]),
                ),
                Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
                  Padding(
                    padding: const EdgeInsets.only(top: 18.0),
                    child: Text(
                      "Statistics".tr,
                      style: Theme.of(context).textTheme.displayLarge?.copyWith(
                            fontSize: 18.0,
                            color: Get.isDarkMode
                                ? Get.theme.primaryColorDark
                                : Colors.black.withOpacity(0.6),
                          ),
                    ),
                  ),
                  Container(
                    margin: EdgeInsets.only(top: 12.0),
                    decoration: BoxDecoration(
                      color: Get.isDarkMode
                          ? AppTheme.colors.darkTheme2
                          : Colors.white.withOpacity(0.3),
                      borderRadius: BorderRadius.circular(3.0),
                    ),
                    child: _reportsController.obx(
                      (data) => Container(
                        height: 200,
                        child: StudentsStatisticsDataGrid(
                            items: _reportsController.studentStatics),
                      ),
                    ),
                  ),
                ]),
                Padding(
                  padding: const EdgeInsets.only(top: 18.0),
                  child: Text(
                    "Current Homework".tr,
                    style: Theme.of(context).textTheme.displayLarge?.copyWith(
                          fontSize: 18.0,
                          color: Get.isDarkMode
                              ? Get.theme.primaryColorDark
                              : Colors.black.withOpacity(0.6),
                        ),
                  ),
                ),
                _reportsController.obx((data) {
                  if (_reportsController.classstudentreport.value.response !=
                      null) {
                    var homeworks = _reportsController
                        .classstudentreport.value.response!.list!
                        .where(
                            (element) => element.assignment?.status == 'Live')
                        .toList();
                    return Container(
                        height: 300, child: CurrentHomeworkDataGrid(homeworks)
                        // _reportsController
                        //   .classstudentreport.value.response!.list!
                        );
                  } else {
                    return Text("Data loading");
                  }
                }),
                Text(
                  "Recent Read".tr,
                  style: Theme.of(context).textTheme.displayLarge?.copyWith(
                        fontSize: 18.0,
                        color: Get.isDarkMode
                            ? Get.theme.primaryColorDark
                            : Colors.black.withOpacity(0.6),
                      ),
                ),
                Obx(() {
                  if (_reportsController.studentHistoryResultBook.isNotEmpty)
                    return Container(
                      decoration: BoxDecoration(
                        border: Border.all(
                          color: const Color(0xff7c94b6),
                          width: 0.5,
                        ),
                        borderRadius: BorderRadius.circular(12),
                      ),
                      height: 330,
                      width: context.width - 36,
                      child: BookSliderGridNoBar2(
                          books: _reportsController.studentHistoryResultBook),
                    );
                  else
                    return Container(
                      height: 330,
                      alignment: Alignment.center,
                      margin: EdgeInsets.only(top: 12.0),
                      child: Text(
                        'No Recent Read Books'.tr,
                        style: TextStyle(
                          color: Get.isDarkMode
                              ? Get.theme.primaryColorDark
                              : Colors.black.withOpacity(0.6),
                        ),
                      ),
                    );
                }),
              ],
            )));
  }
}
